package com.zzq.my.jetpacks.jectpackapp.net.login

import com.zzq.my.jetpacks.jectpackapp.bean.LoginInfokt
import com.zzq.my.jetpacks.jectpackapp.net.ApiService
import com.zzq.my.jetpacks.jectpackapp.net.ApiUrl
import com.zzq.my.jetpacks.jectpackapp.utils.EncRSA
import com.zzq.my.jetpacks.net.base.BaseRepository
import com.zzq.my.jetpacks.net.base.RetrofitClient
import com.zzq.my.jetpacks.net.entity.ApiResponse
import rxhttp.RxHttp
import rxhttp.awaitResult
import rxhttp.toResponse

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2021/11/5-16:24
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class LoginRepo : BaseRepository() {
    //延迟初始化
    // RetrofitClient.getInstance().initConfig("http://122.9.113.19:8006/").create(ApiService::class.java)
    private val mApiService by lazy {

//        RetrofitClient.getInstance().initConfig("http://122.9.113.19:8006/").create(ApiService::class.java)
        RetrofitClient.getInstance().initConfig("http://cs-ys-agentapi2.51ydmw.com/")
            .create(ApiService::class.java)
    }

    /**
     * 获取登录信息
     */
    suspend fun login(userName: String?, password: String?): ApiResponse<LoginInfokt.Data> {
        val requestMap = mutableMapOf<String, String>()//可变的map
        userName?.let { requestMap.put("userName", it) }
        //加密
        password?.let { requestMap.put("password", EncRSA.EncPass(it)) }
        requestMap["agentOem"] = "200010"
        return executeHttp { mApiService.login(requestMap) }
    }

    /**
     * rxhttp 协程请求数据  返回Result<T>
     */
    suspend fun login2(userName: String?, password: String?): Result<LoginInfokt.Data> {
        val requestMap = getPubParams()//公共数据map
        userName?.let { requestMap.put("userName", it) }
        //加密
        password?.let { requestMap.put("password", EncRSA.EncPass(it)) }
        requestMap["agentOem"] = "200010"
        return RxHttp.postJson(ApiUrl.LOGIN)
            .addAll(requestMap)
            .toResponse<LoginInfokt.Data>()//注：toClass()方法是万能的，你可以传递任意数据类型过去 .toClass<List<Student>>()
            .awaitResult()


    }

}